package org.darcy.youchat.activity.message;

import android.content.Intent;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kyleduo.switchbutton.SwitchButton;

import org.darcy.youchat.imageloader.MyImageLoader;
import org.darcy.youchat.library.adapter.LQRAdapterForRecyclerView;
import org.darcy.youchat.library.adapter.LQRViewHolderForRecyclerView;
import org.darcy.youchat.library.recyclerview.YouChatRecyclerView;
import com.netease.nimlib.sdk.InvocationFuture;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.team.constant.TeamMemberType;
import com.netease.nimlib.sdk.team.model.Team;
import com.netease.nimlib.sdk.team.model.TeamMember;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;

import org.darcy.youchat.R;
import org.darcy.youchat.activity.BaseActivity;
import org.darcy.youchat.activity.home.TeamChatCreateActivity;
import org.darcy.youchat.activity.me.myinfo.QRCodeCardActivity;
import org.darcy.youchat.databinding.ActivityTeamChatInfoBinding;
import org.darcy.youchat.library.optionitemview.OptionItemView;
import org.darcy.youchat.model.UserCache;
import org.darcy.youchat.nimsdk.NimHistorySDK;
import org.darcy.youchat.nimsdk.NimRecentContactSDK;
import org.darcy.youchat.nimsdk.NimTeamSDK;
import org.darcy.youchat.nimsdk.NimUserInfoSDK;
import org.darcy.youchat.utils.StringUtils;
import org.darcy.youchat.utils.UIUtils;
import org.darcy.youchat.view.CustomDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 群聊信息界面
 */
public class TeamChatInfoActivity extends BaseActivity {

    private ActivityTeamChatInfoBinding binding;

    public static final String GROUP_CHEAT_INFO_TEAMID = "teamId";
    public static final int REQ_ADD_MEMBERS = 1000;
    public static final int REQ_REMOVE_MEMBERS = 1001;
    public static final int REQ_CHANGE_NAME = 1002;
    public static final int REQ_WATCH_USER_INFO = 1003;
    public static final int RESP_QUIT_TEAM = 10000;
    public static final int RESP_CHEAT_SINGLE = 10001;
    public static final int RESP_CLEAR_CHATTING_RECORD_HISTORY = 10002;

    private Intent mIntent;
    private String mTeamId;
    private Team mTeam;
    private boolean mIsClearChattingHistory = false;
    private boolean mIsManager;//标记当前用户是不是管理员

    private Observer<List<TeamMember>> memberRemoveObserver;
    private Observer<List<TeamMember>> memberUpdateObserver;

    private List<TeamMember> mTeamMemberList = new ArrayList<>();
    private LQRAdapterForRecyclerView mAdapter;
    private CustomDialog mDialog;


    YouChatRecyclerView mRvMember;

    OptionItemView mOivTeamName;

    TextView mTvAnnouncement;

    OptionItemView mOivNickNameInTeam;

    LinearLayout mLlShowNickName;

    SwitchButton mSbShowNickName;


    @Override
    public void init() {
        mTeamId = getIntent().getStringExtra(TeamChatInfoActivity.GROUP_CHEAT_INFO_TEAMID);
        if (TextUtils.isEmpty(mTeamId)) {
            interrupt();
            return;
        }
        mTeam = NimTeamSDK.queryTeamBlock(mTeamId);
        TeamMemberType myMemberType = NimTeamSDK.queryTeamMemberBlock(mTeamId, UserCache.getAccount()).getType();
        mIsManager = myMemberType == TeamMemberType.Manager || myMemberType == TeamMemberType.Owner;

        //监听群成员变化
        observeMemberUpdate();
        observeMemberRemove();
    }

    @Override
    public void initView() {
        binding = ActivityTeamChatInfoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.mToolbar.toolbar);
        getSupportActionBar().setTitle("群聊");
        binding.mToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);
        binding.mToolbar.btnOk.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTeam = NimTeamSDK.queryTeamBlock(mTeamId);
        getSupportActionBar().setTitle("群聊信息(" + mTeam.getMemberCount() + ")");
        mOivTeamName.setRightText(TextUtils.isEmpty(mTeam.getName()) ? "未命名" : mTeam.getName());
        mOivNickNameInTeam.setRightText(NimTeamSDK.getTeamMemberDisplayNameWithoutMe(mTeamId, UserCache.getAccount()));
        if (!TextUtils.isEmpty(mTeam.getAnnouncement())) {
            mTvAnnouncement.setVisibility(View.VISIBLE);
            mTvAnnouncement.setText(mTeam.getAnnouncement());
        } else {
            mTvAnnouncement.setVisibility(View.GONE);
        }
        //是否显示群昵称
        mSbShowNickName.setChecked(NimTeamSDK.shouldShowNickName(mTeamId));
    }

    @Override
    public void initData() {
        //查询本群中成员
        NimTeamSDK.queryMemberList(mTeamId, new RequestCallback<List<TeamMember>>() {
            @Override
            public void onSuccess(List<TeamMember> param) {
                if (!StringUtils.isEmpty(param)) {
                    mTeamMemberList.clear();
                    mTeamMemberList.addAll(param);
                    if (mIsManager) {
                        mTeamMemberList.add(null);
                        mTeamMemberList.add(null);
                    } else {
                        mTeamMemberList.add(null);
                    }
                    //更新本地群成员资料
                    List<String> accountList = new ArrayList<>(param.size());
                    for (TeamMember tm : param) {
                        accountList.add(tm.getAccount());
                    }
                    if (!StringUtils.isEmpty(accountList)) {
                        NimUserInfoSDK.getUserInfosFormServer(accountList, new RequestCallback<List<NimUserInfo>>() {
                            @Override
                            public void onSuccess(List<NimUserInfo> param) {
                                setAdapter();
                            }

                            @Override
                            public void onFailed(int code) {
                                UIUtils.showToast("获取群成员信息失败" + code);
                            }

                            @Override
                            public void onException(Throwable exception) {
                                exception.printStackTrace();
                            }
                        });
                    }
                }
            }

            @Override
            public void onFailed(int code) {
                UIUtils.showToast("查询成员列表失败" + code);
            }

            @Override
            public void onException(Throwable exception) {
                exception.printStackTrace();
            }
        });
    }

    @Override
    public void initListener() {
        binding.oivTeamName.setOnClickListener(v -> {
            mIntent = new Intent(this, TeamNameSetActivity.class);
            mIntent.putExtra(TeamNameSetActivity.TEAM_ID, mTeamId);
            startActivity(mIntent);
        });
        binding.oivQRCordCard.setOnClickListener(v -> {
            mIntent = new Intent(this, QRCodeCardActivity.class);
            mIntent.putExtra(QRCodeCardActivity.QRCODE_TEAM, mTeam);
            startActivity(mIntent);
        });
        binding.llAnnouncement.setOnClickListener(v -> {
            if (mIsManager) {
                //编辑公告
                mIntent = new Intent(this, TeamAnnouncementEditActivity.class);
                mIntent.putExtra(TeamAnnouncementEditActivity.TEAM, mTeam);
                startActivity(mIntent);
            } else {
                showMaterialDialog("", "只有群主可以编辑群公告", "知道了", "", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        hideMaterialDialog();
                    }
                }, null);
            }
        });
        binding.oivNickNameInTeam.setOnClickListener(v -> {
            showChangeNickNameDialog();
        });
        binding.btnQuitTeam.setOnClickListener(v -> {
            showWaitingDialog("请稍等");
            NimTeamSDK.quitTeam(mTeamId, new RequestCallback<Void>() {
                @Override
                public void onSuccess(Void param) {
                    hideWaitingDialog();
                    //退出群聊时顺便清除本地最后联系人中的群消息
                    NimRecentContactSDK.deleteRecentContactAndNotify(mTeamId, SessionTypeEnum.Team);
                    setResult(RESP_QUIT_TEAM);
                    onBackPressed();
                }

                @Override
                public void onFailed(int code) {
                    hideWaitingDialog();
                    UIUtils.showToast("退群失败" + code);
                }

                @Override
                public void onException(Throwable exception) {
                    hideWaitingDialog();
                    UIUtils.showToast("退群失败");
                    exception.printStackTrace();
                }
            });
        });
        binding.oivClearMsgRecord.setOnClickListener(v -> showMaterialDialog("", "确定删除群的聊天记录吗?", "清空", "取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideMaterialDialog();
                NimHistorySDK.clearChattingHistory(mTeamId, SessionTypeEnum.Team);
                mIsClearChattingHistory = true;
            }
        }, v2 -> hideMaterialDialog()));

        mLlShowNickName.setOnClickListener(v -> mSbShowNickName.setChecked(!mSbShowNickName.isChecked()));
        mSbShowNickName.setOnCheckedChangeListener((buttonView, isChecked) -> {
            showWaitingDialog("请稍等");
            NimTeamSDK.setShouldShowNickName(mTeamId, isChecked, new RequestCallback<Void>() {
                @Override
                public void onSuccess(Void param) {
                    hideWaitingDialog();
                }

                @Override
                public void onFailed(int code) {
                    UIUtils.showToast("设置失败" + code);
                    hideWaitingDialog();
                }

                @Override
                public void onException(Throwable exception) {
                    exception.printStackTrace();
                    hideWaitingDialog();
                }
            });
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQ_ADD_MEMBERS:
                if (resultCode == RESULT_OK) {
                    showWaitingDialog("请稍等");
                    //拉人入群
                    ArrayList<String> accounts = data.getStringArrayListExtra(TeamChatCreateActivity.ADD_TEAM_MEMBER);
                    NimTeamSDK.addMembers(mTeamId, accounts, new RequestCallback<List<String>>() {
                        @Override
                        public void onSuccess(List<String> result) {
                            hideWaitingDialog();
                        }

                        @Override
                        public void onFailed(int code) {
                            UIUtils.showToast("拉人入群失败" + code);
                            hideWaitingDialog();
                        }

                        @Override
                        public void onException(Throwable exception) {
                            exception.printStackTrace();
                            UIUtils.showToast("拉人入群失败");
                            hideWaitingDialog();
                        }
                    });
                }
                break;
            case REQ_REMOVE_MEMBERS:
                if (resultCode == RESULT_OK) {
                    showWaitingDialog("请稍等");
                    ArrayList<String> accounts = data.getStringArrayListExtra(TeamChatRemoveMemberActivity.REMOVE_TEAM_MEMBER);
                    InvocationFuture<Void> invocationFuture = NimTeamSDK.removeMembers(mTeamId, accounts);
                    invocationFuture.setCallback(new RequestCallback<Void>() {
                        @Override
                        public void onSuccess(Void param) {
                            hideWaitingDialog();
                        }

                        @Override
                        public void onFailed(int code) {
                            UIUtils.showToast("踢人出群失败" + code);
                            hideWaitingDialog();
                        }

                        @Override
                        public void onException(Throwable exception) {
                            exception.printStackTrace();
                            UIUtils.showToast("踢人出群失败");
                            hideWaitingDialog();
                        }
                    });
                }
                break;
            case REQ_WATCH_USER_INFO:
                if (resultCode == RESULT_OK) {
                    setResult(RESP_CHEAT_SINGLE);
                    onBackPressed();
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NimTeamSDK.observeMemberUpdate(memberUpdateObserver, false);
        NimTeamSDK.observeMemberRemove(memberRemoveObserver, false);
    }

    @Override
    public void onBackPressed() {
        if (mIsClearChattingHistory)
            setResult(RESP_CLEAR_CHATTING_RECORD_HISTORY);
        super.onBackPressed();
    }


    private void setAdapter() {
        if (mAdapter == null) {
            mAdapter = new LQRAdapterForRecyclerView(this, R.layout.item_member_info_group_cheat_rv, mTeamMemberList) {
                @Override
                public void convert(LQRViewHolderForRecyclerView helper, Object obj, int position) {
                    final ImageView ivHeader = helper.getView(R.id.ivHeader);
                    if (mIsManager && position >= mTeamMemberList.size() - 2) {//+和-
                        if (position == mTeamMemberList.size() - 2) {//+
                            ivHeader.setImageResource(R.mipmap.ic_add_team_member);
                            helper.getView(R.id.root).setOnClickListener(v -> {
                                //拉人入群
                                addMembers();
                            });
                        } else {//-
                            helper.getView(R.id.root).setOnClickListener(v -> {
                                //踢人出群
                                removeMember();
                            });
                            ivHeader.setImageResource(R.mipmap.ic_remove_team_member);
                        }
                        helper.setText(R.id.tvName, "");
                    } else if (!mIsManager && position >= mTeamMemberList.size() - 1) {//+
                        helper.getView(R.id.root).setOnClickListener(v -> {
                            //拉人入群
                            addMembers();
                        });
                        ivHeader.setImageResource(R.mipmap.ic_add_team_member);
                        helper.setText(R.id.tvName, "");
                    } else {
                        final TeamMember item = (TeamMember) obj;
                        helper.getView(R.id.root).setOnClickListener(v -> {
                            //查看对方资料
                            Intent intent = new Intent(TeamChatInfoActivity.this, UserInfoActivity.class);
                            intent.putExtra(UserInfoActivity.USER_INFO_ACCOUNT, item.getAccount());
                            startActivityForResult(intent, REQ_WATCH_USER_INFO);
                        });
                        helper.setText(R.id.tvName, NimTeamSDK.getTeamMemberDisplayNameWithoutMe(item.getTid(), item.getAccount()));
                        String account = item.getAccount();
                        NimUserInfo userInfo = NimUserInfoSDK.getUser(account);
                        if (userInfo == null) {
                            NimUserInfoSDK.getUserInfoFromServer(account, new RequestCallback<List<NimUserInfo>>() {
                                @Override
                                public void onSuccess(List<NimUserInfo> param) {
                                    if (!StringUtils.isEmpty(param)) {
                                        NimUserInfo userInfo = param.get(0);
                                        if (!TextUtils.isEmpty(userInfo.getAvatar())) {
                                            MyImageLoader.LoadNetImage(userInfo.getAvatar(), ivHeader);
                                        } else {
                                            ivHeader.setImageResource(R.mipmap.default_header);
                                        }
                                    }
                                }

                                @Override
                                public void onFailed(int code) {
                                    ivHeader.setImageResource(R.mipmap.default_header);
                                }

                                @Override
                                public void onException(Throwable exception) {
                                    ivHeader.setImageResource(R.mipmap.default_header);
                                }
                            });
                        } else {
                            if (!TextUtils.isEmpty(userInfo.getAvatar())) {
                                MyImageLoader.LoadNetImage(userInfo.getAvatar(), ivHeader);
                            } else {
                                ivHeader.setImageResource(R.mipmap.default_header);
                            }
                        }
                    }
                }
            };
            mRvMember.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 拉人入群
     */
    private void addMembers() {
        //得到群成员账号集合
        ArrayList<String> selectedTeamMemberAccounts = new ArrayList<>();
        for (int i = 0; i < mTeam.getMemberCount(); i++) {
            selectedTeamMemberAccounts.add(mTeamMemberList.get(i).getAccount());
        }

        Intent intent = new Intent(this, TeamChatCreateActivity.class);
        intent.putStringArrayListExtra(TeamChatCreateActivity.ADD_TEAM_MEMBER, selectedTeamMemberAccounts);
        startActivityForResult(intent, REQ_ADD_MEMBERS);
    }

    /**
     * 踢人出群
     */
    private void removeMember() {
        Intent intent = new Intent(this, TeamChatRemoveMemberActivity.class);
        intent.putExtra(TeamChatRemoveMemberActivity.TEAMID, mTeamId);
        startActivityForResult(intent, REQ_REMOVE_MEMBERS);
    }

    private void observeMemberUpdate() {
        memberUpdateObserver = (Observer<List<TeamMember>>) teamMembers -> {
            initData();
            onResume();
        };
        NimTeamSDK.observeMemberUpdate(memberUpdateObserver, true);
    }

    private void observeMemberRemove() {
        memberRemoveObserver = teamMembers -> {
            initData();
            onResume();
        };
        NimTeamSDK.observeMemberRemove(memberRemoveObserver, true);
    }

    private void showChangeNickNameDialog() {
        View view = View.inflate(this, R.layout.dialog_team_nick_change, null);
        mDialog = new CustomDialog(this, view, R.style.dialog);
        mDialog.setCancelable(false);
        mDialog.show();
        final EditText etName = view.findViewById(R.id.etName);
        String nickName = NimTeamSDK.getTeamMemberDisplayNameWithoutMe(mTeamId, UserCache.getAccount());
        etName.setText(nickName);
        if (!TextUtils.isEmpty(nickName) && nickName.length() > 0)
            etName.setSelection(nickName.length());
        view.findViewById(R.id.tvCancle).setOnClickListener(v -> {
            mDialog.dismiss();
            mDialog = null;
        });
        view.findViewById(R.id.tvOk).setOnClickListener(v -> {
            //修改我在群中的昵称
            String newNickName = etName.getText().toString().trim();
            if (!TextUtils.isEmpty(newNickName)) {
                NimTeamSDK.updateMyTeamNick(mTeamId, newNickName, new RequestCallback<Void>() {
                    @Override
                    public void onSuccess(Void param) {
                        UIUtils.showToast("修改成功");
                        mDialog.dismiss();
                        mDialog = null;
                        onResume();
                    }

                    @Override
                    public void onFailed(int code) {
                        if (code == 805) {
                            UIUtils.showToast("网易云信的普通群不支持修改自己的群昵称");
                        } else {
                            UIUtils.showToast("修改失败" + code);
                        }
                    }

                    @Override
                    public void onException(Throwable exception) {
                        UIUtils.showToast("修改失败");
                        exception.printStackTrace();
                    }
                });
            }
        });
    }
}
